﻿///////////////////////////////////////////////////////////////////////////////
//	Copyright 2013 JASDev International
//
//	Licensed under the Apache License, Version 2.0 (the "License");
//	you may not use this file except in compliance with the License.
//	You may obtain a copy of the License at
//
//		http://www.apache.org/licenses/LICENSE-2.0
//
//	Unless required by applicable law or agreed to in writing, software
//	distributed under the License is distributed on an "AS IS" BASIS,
//	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//	See the License for the specific language governing permissions and
//	limitations under the License.
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Text;

using JDI.Common.Utils;

namespace JDI.Pusher.Client
{
	/// <summary>
	/// Represents a Pusher Presence channel.
	/// </summary>
	public class PusherPresenceChannel : PusherPrivateChannel
	{
		#region Constructors and IDisposable

		internal PusherPresenceChannel(string channelName, PusherClient pusher) : base(channelName, pusher)
		{
			this.channelType = PusherChannelType.Presence;
			this.members = null;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.members != null)
					this.members.Dispose();
			}

			this.members = null;

			base.Dispose(disposing);
		}

		#endregion


		#region Properties

		public PusherMembers Members
		{
			// TODO - implement PusherChannel::Members property
			get { return this.members; }
		}

		#endregion


		#region Internal Methods

		internal override void Subscribe(PusherDelegates.ChannelStatusCallback callback = null)
		{
			this.statusCallback = callback;

			this.status = PusherChannelState.Authorizing;

			if (this.statusCallback != null)
			{
				this.statusCallback(this.status);
			}

			// do authorization then subscribe
			/*
			using (var webClient = new System.Net.WebClient())
			{
				string data = String.Format("channel_name={0}&socket_id={1}", channelName, this.socketID);

				webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
				webClient.UploadStringCompleted += webClient_UploadStringCompleted;
				webClient.UploadStringAsync(new Uri(ChannelAuthEndpoint), null, data, this.channels[channelName]);
			}
			*/
	
			// TODO - implement this properly
			this.pusher.SendSubscribeRequest(this.name, "authkey", "channeldata");
		}

		internal override void SubscriptionSucceeded(string data)
		{
			// get member list
			this.members = ParseMemberList(data);

			base.SubscriptionSucceeded(data);
		}

		internal override void SubscriptionFailed(string data)
		{
			// TODO - finish this
			base.SubscriptionFailed(data);
		}

		internal void AddMember(PusherMessage.MemberData member)
		{
			this.members.Add(member);

			// TODO - finish this
			//	if (this.MemberAdded != null)
			//		this.AddMember(this);
		}

		internal void RemoveMember(string userId)
		{
			this.members.Remove(userId);

			// TODO - finish this
			//	if (this.MemberRemoved != null)
			//		this.RemoveMember(this);
		}

		#endregion


		#region Private Methods

		private PusherMembers ParseMemberList(string data)
		{
			return new PusherMembers();
		}

		#endregion


		#region Member Fields

		protected PusherMembers members;

		#endregion
	}
}
